package com.edu.robot.modules.robot.corvert;

import cn.hutool.json.JSONUtil;
import com.edu.robot.modules.robot.controller.vo.RobotVO;
import com.edu.robot.modules.robot.dto.RobotConfigDTO;
import com.edu.robot.modules.robot.dto.RobotInfoDTO;
import com.edu.robot.modules.robot.model.CrRobot;

import java.util.Optional;

/**
 * @program: KQAIA
 * @description:
 * @author: 无恙
 * @create: 2025-03-23 11:55
 **/


public class RobotCovert {

	public static CrRobot convert(RobotVO robotVO) {
		//2.保存机器人
		RobotConfigDTO robotConfigDTO = RobotConfigDTO.builder()
				.modelConfigId(robotVO.getModelId())
				.promptId(robotVO.getPromptId())
				.publicKnowledgeId(robotVO.getKnowledgeIds())
				.toolConfigId(robotVO.getToolIds())
				.build();


		CrRobot crRobot = new CrRobot();
		Optional.ofNullable(robotVO.getId()).ifPresent(id->crRobot.setId(Long.valueOf(id)));
		crRobot.setName(robotVO.getName());
		crRobot.setRobotConfig(JSONUtil.toJsonStr(robotConfigDTO));
		crRobot.setDescription(robotVO.getDescription());
		crRobot.setRobotLogo(robotVO.getLogo());
		crRobot.setStatus(1);
		return crRobot;
	}

	public static RobotInfoDTO convert(CrRobot crRobot){
		RobotInfoDTO robotInfoDTO = new RobotInfoDTO();
		try {
			robotInfoDTO.setRobotId(String.valueOf(crRobot.getId()));
			robotInfoDTO.setRobotDesc(crRobot.getDescription());
			robotInfoDTO.setRobotName(crRobot.getName());
			robotInfoDTO.setRobotLogo(crRobot.getRobotLogo());
			robotInfoDTO.setRobotCreator(crRobot.getCreatedBy());
			robotInfoDTO.setRobotConfig(JSONUtil.toBean(crRobot.getRobotConfig(), RobotConfigDTO.class));
			robotInfoDTO.setStatus(String.valueOf(crRobot.getStatus()));
		} catch (NullPointerException e) {
			throw new IllegalArgumentException("Input 'crRobot' is null or contains null values", e);
		} catch (Exception e) {
			throw new RuntimeException("An unexpected error occurred while converting CrRobot to RobotInfoDTO", e);
		}
		return robotInfoDTO;
	}
}
